package com.twp.auth.rest.api.manage;

import com.twp.auth.domain.authority.Role;
import com.twp.auth.service.manage.RoleService;
import com.twp.common.component.process.exception.combo.IComboExecptionForTFunc;
import com.twp.common.tuple.ResultDTO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "api/oauth/role",produces = "application/json; charset=utf-8")
public class RoleControler {

    @Autowired
    RoleService roleService;

    @Autowired
    IComboExecptionForTFunc iComboExecptionForTFunc;

    @GetMapping("")
    ResultDTO find(Role role, @PageableDefault Pageable pageable){
        return iComboExecptionForTFunc.executeFlow(
                ResultDTO.builder().message("查询角色成功").build(),
                it->it.setData(roleService.find(role,pageable)),
                (e,t)->{
                    t.setIsSuccess(false);
                    t.setMessage("查询角色失败");
                }
        );
    }

    @DeleteMapping("/{id}")
    ResultDTO delete(@PathVariable String id){
       return iComboExecptionForTFunc.executeFlow(
               ResultDTO.builder().message("删除角色成功").build(),
                it-> roleService.deleteById(Integer.valueOf(id)),
               (e,it)->{
                    it.setMessage("删除角色失败");
                    it.setIsSuccess(false);
                }
        );
    }

    @PostMapping("")
    ResultDTO action(@RequestBody Role role){
        return iComboExecptionForTFunc.executeFlow(
                ResultDTO.builder().message(role.getId()==null?"新增角色成功":"修改角色成功").build(),
                it->roleService.saveOrUpdate(role),
                (e,it)->{
                    it.setMessage("操作失败");
                    it.setIsSuccess(false);
                }
        );
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class ActionBody{
        private Integer roleId;
        private List<Integer> menuIds;
    }

    /*
    取消角色与菜单的绑定
    增加角色与菜单绑定
     */
    @PutMapping("action")
    ResultDTO action(@RequestBody ActionBody actionBody){
        return iComboExecptionForTFunc.executeFlow(
                ResultDTO.builder().message("操作角色与菜单绑定").build(),
                it-> roleService.action(actionBody),
                (e,it)->{
                    it.setMessage("操作角色与菜单绑定异常");
                    it.setIsSuccess(false);
                }
        );
    }

    @GetMapping("loadData")
    ResultDTO loadData(@RequestParam String userId,String roleName,String roleCode,@PageableDefault Pageable pageable){
        return iComboExecptionForTFunc.executeFlow(
                ResultDTO.builder().message("(角色)菜单获取成功").build(),
                it-> {
                    it.setData(roleService.loadData(Integer.parseInt(userId),roleName,roleCode,pageable));
                },
                (e,it)->{
                    it.setMessage("(角色)菜单获取失败");
                    it.setIsSuccess(false);
                }
        );
    }
}
